package edu.rjh.reflection.reflection_2;

import java.lang.reflect.Field;

public class Test {
    public static void main(String[] args) throws
            ClassNotFoundException,
            NoSuchFieldException,
            IllegalAccessException {
        Class<?> clazz = Class.forName("edu.rjh.reflection.reflection_2.Student");

        //获取所有(包括私有)的成员变量
//        Field[] fields = clazz.getDeclaredFields();
//        for (Field field : fields) {
//            System.out.println(field);
//        }

        //获取单个的成员变量
        Field field = clazz.getDeclaredField("name");
        System.out.println(field);

        //获取权限修饰符
        int modifiers = field.getModifiers();
        System.out.println(modifiers);

        //获取成员变量的名字
        String name = field.getName();
        System.out.println(name);

        //获取成员变量的数据类型
        Class<?> type = field.getType();
        System.out.println(type);

        //获取成员变量的记录值
        Student student = new Student("男", 18, "rjh");
        //设置权限
        field.setAccessible(true);
        Object value = field.get(student);
        System.out.println(value);

        //修改成员变量的值
        field.set(student,"rjh222");
        System.out.println(student);
    }
}
